package main.java.pers.seg.model.utils;

import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.List;

import static main.java.pers.seg.model.constants.ObjectConstants.viewcomponentmap;
import static main.java.pers.seg.model.constants.ObjectConstants.viewcontainermap;

public class NodeUtils {

    public static boolean judgeElement(Element ifmlnode){
        boolean result = true;
        String parentType = ifmlnode.getParent().attributeValue("xsi:type");
        String currentType = ifmlnode.attributeValue("xsi:type");
        //viewComponents嵌套viewComponents ，暂时约定为非法情况
        if(viewcomponentmap.containsValue(parentType)
            && (viewcomponentmap.containsValue(currentType) || currentType.equals("ext:Icon"))){
            result = false;
        }
        //viewContainer嵌套viewContainer,子节点变为viewElements
        else if(viewcontainermap.containsValue(parentType) && viewcontainermap.containsValue(currentType)){
            ifmlnode.setName("viewElements");

        }
        else if(ifmlnode.getParent().getName().equals("interactionFlowModel")
                && viewcontainermap.containsValue(currentType)){
            ifmlnode.setName("interactionFlowModelElements");
        } else if (viewcontainermap.containsValue(parentType)
                && (viewcomponentmap.containsValue(currentType) || currentType.equals("ext:Icon"))) {
            ifmlnode.setName("viewElements");

        }
        return result;
    }
    public static boolean judgeElement2(Element ifmlnode){
        boolean result = true;
        String parentType = ifmlnode.getParent().attributeValue("type");
        String currentType = ifmlnode.attributeValue("type");
        //viewComponents嵌套viewComponents ，暂时约定为非法情况
        if(viewcomponentmap.containsValue(parentType)
                && (viewcomponentmap.containsValue(currentType) || currentType.equals("ext:Icon"))){
            result = false;
        }
        //viewContainer嵌套viewContainer,子节点变为viewElements
        else if(viewcontainermap.containsValue(parentType) && viewcontainermap.containsValue(currentType)){
            ifmlnode.setName("viewElements");

        }
        else if(ifmlnode.getParent().getName().equals("interactionFlowModel")
                && viewcontainermap.containsValue(currentType)){
            ifmlnode.setName("interactionFlowModelElements");
        } else if (viewcontainermap.containsValue(parentType)
                && (viewcomponentmap.containsValue(currentType) || currentType.equals("ext:Icon"))) {
            ifmlnode.setName("viewElements");

        }
        return result;
    }

    /**
     * 向上找父Screen
     * @param node
     * @return
     */
   /* public static Element findParentScreen(Element node){
        List<Attribute> list = node.attributes();

        while(list.get(0).getValue().indexOf("Activity")==-1){
            Element e = node.getParent();
            node = e;
            findParentScreen(e);
            list=node.attributes();
        }
        //
        return node;
    }*/

    /*public static Element findParentScreen(Element node){
        Element parentNode = node.getParent();
        if(parentNode.attribute("xsi:type")!=null){
            while(!viewcontainermap.containsValue(parentNode.attributeValue("xsi:type"))
                    && !parentNode.getName().equals("interactionFlowModel")){
                //node = parentNode;
                findParentScreen(parentNode);
            }
        }
        return parentNode;
    }*/

  /*  public static Element findParentScreen(Element node){
        Element parentNode = node.getParent();
        if(parentNode.attribute("xsi:type")!=null){
            while(!viewcontainermap.containsValue(parentNode.attributeValue("xsi:type"))
                    && !parentNode.getName().equals("interactionFlowModel")){
                //node = parentNode;
                findParentScreen(parentNode);
            }
        }
        return parentNode;
    }*/

    public static Element findParentScreen(Element node){
        Element parentNode = node.getParent();
        if(parentNode.attribute("type")!=null){
            while(!viewcontainermap.containsValue(parentNode.attributeValue("xsi:type"))
                    && !parentNode.getName().equals("interactionFlowModel")){
                //node = parentNode;
                findParentScreen(parentNode);
            }
        }
        return parentNode;
    }
}
